﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;
using System.IO;

public partial class Admin_ThemBaiViet : System.Web.UI.Page
{
    EDiamondEntities1 db = new EDiamondEntities1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["IDBV"]))
            {
                int id = Convert.ToInt32(Request.QueryString["IDBV"]);
                BaiViet bv = db.BaiViets.FirstOrDefault(x => x.BaiVietID == id);
                if (bv != null)
                {
                    dsTheloai.SelectedValue = bv.TheLoai.ToString();
                    txtMaBai.Text = Convert.ToString(bv.BaiVietID);
                    TextBox1.Text = bv.TuaBaiViet;
                    txtngayCapNhat.Text = string.Format("{0:dd/MM/yyyy}", bv.NgayCapNhat);
                    txtHinh.Value = bv.HinhDaiDien;  // html server control
                    txtBiDanh.Text = bv.BiDanh;
                    CKEditorControl1.Text = bv.NoiDung;
                    imgHinh.Src = "../Phots/Baiviet/" + bv.HinhDaiDien;  // html server control
                }
            }
            TieuDe.Text = (!string.IsNullOrEmpty(Request.QueryString["IDBaiBiet"]) ? "Cap nhat" : "Them") + "Bai viet";
        }
        DirectoryInfo tm = new DirectoryInfo(Server.MapPath("~/Photos/Baiviet"));
        FileInfo[] dsfile = tm.GetFiles("*.jpg").Union(tm.GetFiles("*.png")).ToArray();
        dsHinh.DataTextField = "Name";
        dsHinh.DataValueField = "Name";
        dsHinh.DataSource = dsfile;
        dsHinh.DataBind();

    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        BaiViet bv = new BaiViet();
        string bdanh = Thuvien.BoDauTiengViet(TextBox1.Text);
        if(!string.IsNullOrEmpty(Request.QueryString["IDBV"]))
        {
            int id = Convert.ToInt32(Request.QueryString["IDBV"]);
            bv = db.BaiViets.FirstOrDefault(x => x.BaiVietID == id);
        }
        bv.TheLoai = Convert.ToInt32(dsTheloai.SelectedValue);
        bv.TuaBaiViet = TextBox1.Text;
        bv.NgayCapNhat = DateTime.Today;

        bv.HinhDaiDien = txtHinh.Value;
        bv.BiDanh = bdanh;
        bv.NoiDung = CKEditorControl1.Text;
        bv.NguoiDungID = Convert.ToInt32(Session["MaQT"]);
        if (!string.IsNullOrEmpty(Request.QueryString["IDBV"]))
        {
            try
            {
                db.SaveChanges();
                ThongBao.Text = "Cap nhat thanh cong";
            }
            catch (Exception ex)
            {
                ThongBao.Text = "Cap nhat khong thanh cong" + ex.Message;
            }
        }
        else // them moi
        {
            try
            {
                db.BaiViets.AddObject(bv);
                db.SaveChanges();
                ThongBao.Text = "Them moi thanh cong";
            }
            catch (Exception ex)
            {
                ThongBao.Text = "them moi khong thanh cong" + ex.Message;
            }
        }
        txtngayCapNhat.Text = string.Format("{0:dd/MM/yyyy}", DateTime.Today);
        ThongBao.Visible = true;
        imgHinh.Src = "../Phots/Baiviet/" + bv.HinhDaiDien;
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
            FileUpload1.SaveAs(Server.MapPath(@"~\Photos\BaiViet\") + FileUpload1.FileName);
    }
}